/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * Contains some code from Prototype Javascript Framework:
 *   copyright (c) 2005 Sam Stephenson <sam@conio.net>
 *   link      http://prototype.conio.net
 *   license   http://www.opensource.org/licenses/mit-license.php The MIT License
 * 
 * @package    FeatherCMS
 * @subpackage Javascript
 * @copyright  Copyright (c) 2006, Julien PORTALIER
 * @link       http://portalier.com/feather
 * @license    http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

var Event = new Class(
{
	KEY_BACKSPACE:  8,
	KEY_TAB:        9,
	KEY_RETURN:    13,
	KEY_ESC:       27,
	KEY_LEFT:      37,
	KEY_UP:        38,
	KEY_RIGHT:     39,
	KEY_DOWN:      40,
	KEY_DELETE:    46,
	KEY_HOME:      36,
	KEY_END:       35,
	KEY_PAGEUP:    33,
	KEY_PAGEDOWN:  34,
	
	initialize: function(e)
	{
		this.event = e || window.event;
	},
	
	target: function()
	{
		var target;
		
		if (this.event.target) {
			target = this.event.target;
		}
		else if (this.event.srcElement) {
			target = this.event.srcElement;
		}
		
		// Safari
		if (target.nodeType == 3) {
			target = target.parentNode;
		}
		return $(target);
	},
	
    stop: function()
	{
        this.stopPropagation();
        this.preventDefault();
		return this;
    },
	
    stopPropagation: function()
	{
        if (this.event.stopPropagation) {
			this.event.stopPropagation();
        } else {
			this.event.cancelBubble = true;
		}
		return this;
    },
	
    preventDefault: function() {
        if (this.event.preventDefault) {
			this.event.preventDefault();
        } else {
			this.event.returnValue = false;
		}
		return this;
    },
	
	// keyboard
	
	key: function()
	{
		var k = {};
		
		if (this.event.keyCode) {
			k.code = this.event.keyCode;
		}
		else if (this.event.which) {
			k.code = this.event.which;
		}
		k.string = String.fromCharCode(k.code);
		return k;
	},
	
	modifier: function(mod)
	{
		return this.event[mod + 'Key'];
	},
	
	// mouse
	
	pointerX: function()
	{
		return this.event.pageX || (this.event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
	},
	
	pointerY: function() {
		return this.event.pageY || (this.event.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
	},
	
	isRightClick: function()
	{
		return (((this.event.which) && (this.event.which == 3)) || ((this.event.button) && (this.event.button == 2)));
	},
	
	wheelDelta: function(e)
	{
		if (e.wheelDelta) {
			return (e.wheelDelta / 120 * (window.opera ? -1 : 1));
		} else if (e.detail) {
			return (-e.detail / 3);
		}
	}
});